#### Replication Code for:
#### Legislative Cooptation in Authoritarian Regimes: Policy Cooperation in the Kuwait National Assembly
#### Authors: Daniel Tavana and Erin York
#### Created: 07/22/24

# this code replicates Figure 1 in the main text
# # this code requires downloading V-Dem Country-Year Data v11.1
# see READ ME file for more details and download link

rm(list = ls())

library(tidyverse)
library(logr)

options("logr.autolog" = TRUE)

# Open the log
lf <- log_open(file.path("logexternal.log"))

# Send message to log
log_code()


# Figure 1 ----------------------------------------------------------------


# load vdem data
vdem <- read.csv(file = "vdem11.1.csv")
vdem2020 <-
  vdem %>%
  filter(year == 2020) %>%
  mutate(
    regime_type = case_when(
      v2x_regime == 0 ~ "Autocracy",
      v2x_regime == 1 ~ "Autocracy",
      v2x_regime == 2 ~ "Democracy",
      v2x_regime == 3 ~ "Democracy")) %>%
  dplyr::select(country_name,regime_type,v2lglegplo_osp,v2x_polyarchy,v2lglegplo_ord,v2dlconslt_ord,e_v2xlg_legcon_4C)

vdem2020 %>%
  dplyr::group_by(regime_type) %>%
  dplyr::summarize(median_edi = median(v2x_polyarchy, na.rm=T),
                   median_lglegplot = median(v2lglegplo_osp, na.rm=T))
vdem2020 %>%
  filter(country_name == "Kuwait") %>%
  dplyr::summarize(median_edi = median(v2x_polyarchy, na.rm=T),
                   median_lglegplot = median(v2lglegplo_osp, na.rm=T))

f1 <- vdem2020 %>%
  filter(country_name != "Armenia") %>%
  ggplot(aes(v2lglegplo_osp, v2x_polyarchy, color=regime_type, shape=regime_type)) +
  theme_bw() +
  labs(x = "Is the approval of the legislature required to pass legislation? (0 = No, 1 = Usually, 2 = Always)",
       y = "Electoral democracy index (0-1)",
       title = "") +
  scale_y_continuous(
    breaks=seq(0.0, 1.0, 0.1),
    minor_breaks = NULL,
    limits = c(0.0, 1.0),
    expand=c(0, 0)) +
  scale_x_continuous(
    breaks=seq(0.0, 2.0, 0.5),
    minor_breaks = NULL,
    limits = c(0.0, 2.0),
    expand=c(0, 0.01)) +
  theme(
    legend.position = "bottom", # place legend on bottom of plot
    legend.key.width = unit(1, "cm"), # set length of color bars in legend
    legend.margin = margin(-10, 0, 0, 0), # set margins
    legend.title = element_blank(), # remove legend title
    plot.title = element_text(hjust = 0.5), # center title
    text = element_text(size = 12, family = "Times"), # set font size
    axis.text.y = element_text(size = 12), # set font size for y-axis tick labels
    axis.text.x = element_text(size = 12), # set font size for y-axis labels
    panel.grid.minor = element_blank(), # no minor grid for x-axis and y-axis
    panel.grid.major.x = element_blank(), # no major grid for x-axis 
    axis.line = element_line(colour = "black")) + # set color for x-axis and y-axis line
  scale_shape_manual(values = c(1, 4), name = "") +
  scale_color_manual(values = c("#FF0000", "#00A08A"), name = "") +
  geom_vline(xintercept=1.68, color = "lightgray") +
  geom_vline(xintercept=1.93, color = "lightgray") +
  geom_hline(yintercept=0.284, color = "lightgray") +
  geom_hline(yintercept=0.746, color = "lightgray") +
  annotate("text", x = 1.58, y = 0.1, label = "Autocracy median = 1.68", family = "Times", size = 3) +
  annotate("text", x = 1.83, y = 0.05, label = "Democracy median = 1.93", family = "Times", size = 3) +
  annotate("text", x = 0.16, y = 0.321, label = "Autocracy median = 0.28", family = "Times", size = 3) +
  annotate("text", x = 0.16, y = 0.787, label = "Democracy median = 0.75", family = "Times", size = 3) +
  geom_text(
    data = subset(vdem2020, country_name == "Kuwait"), color = "black",
    aes(v2lglegplo_osp,.26,label = "Kuwait"), show.legend = FALSE, family = "Times") +
  geom_point(position = position_jitter(seed = 1)) 
f1

ggsave(f1, file = "Figure1.pdf", width = 9, height = 3.75)



####

log_close()
